// Item.java
//
// The basic Item class. It's a very customizable framework!

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.nio.*;

public class Item
{
    //Characteristics of the Item
    private int num; // The ID number of this item
    private String name;
    private String desc;
    private boolean equippable;
    private String type;
    private BufferedImage eImg;  // The overlay for equipped items
    
    // Other stuff
    private BufferedImage img;
    private BufferedReader reader;

    public Item(int itemNum)
    {
        try
        {
            // Set the ID number and default values
            num = itemNum;
            name = "Nothing";
            desc = "This is the lack of an item! ERROR: This shouldn't appear through normal gameplay!";
            img = ImageIO.read(new File("Items\\Images\\Blank.png"));
            eImg = ImageIO.read(new File ("Items\\Images\\Blank.png"));
            equippable = false;
            type = "";
            
            // read each line from the terrain's config file into a temporary string
            // and then set the terrain's variables based on that string
            String file = "Items\\" + num + ".txt";
            reader = new BufferedReader(new FileReader(file));
            String temp;
            String cat, value;
            while(true)
            {
                temp = reader.readLine();
                if (temp.contains("END")) {break;}  
                cat = temp.substring(0, temp.indexOf("="));
                value = temp.substring(temp.indexOf("=") + 1);
                
                switch (cat)
                {
                    case "Name":
                    {
                        name = value;
                        break;
                    }
                    case "Desc":
                    {
                        desc = value;
                        break;
                    }
                    case "Image":
                    {
                        img = ImageIO.read(new File(value));
                        break;
                    }
                    case "Equippable":
                    {
                        equippable = Boolean.valueOf(value);
                        break;
                    }
                    case "Type":
                    {
                        type = value;
                        break;
                    }
                    case "EquipImg":
                    {
                        eImg = ImageIO.read(new File(value));
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            reader.close();
        }
        catch(IOException e)
        {
            System.out.println("Uh oh! Something broke with the file reading!");
            System.out.println(e);
        }
    }

    public void display(int x, int y, Graphics g)
    {
        g.drawImage(img, x, y, null);
    }
    
    public void displayEquip(int x, int y, Graphics g)
    {
        g.drawImage(eImg, x, y, null);
    }

    public void tick()
    {
    }
    
    public boolean isEquippable()
    {
        return equippable;
    }
    
    // Prints the description
    public void examine()
    {
        System.out.println(name.substring(0,1).toUpperCase() + name.substring(1)
                            + ". " + desc);
    }

    // GET AND SET METHODS
    public int getNum()
    {
        return num;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String newName)
    {
        name = newName;
    }

    public String getDesc()
    {
        return desc;
    }

    public void setDesc(String newDesc)
    {
        desc = newDesc;
    }

    public void setImg(BufferedImage newImg)
    {
        img = newImg;
    }
    
    public String getType()
    {
        return type;
    }
    
    public void setType(String newType)
    {
        type = newType;
    }

    // END GETS/SETS

    public String toString()
    {
        return ("///////////////" +
		"\n//Class: Item" +
		"\n//ID Number: " + num +
		"\n//Name: " + name +
		"\n//Description: " + desc +
		"\n///////////////");
    }
}